package com.example.springbootuploadanddownload.controller;

import com.example.springbootuploadanddownload.domain.Files;
import com.example.springbootuploadanddownload.service.FilesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.sql.Blob;

/**
 * 上传到数据库
 * */
@RestController
@Slf4j
@RequestMapping("/db")
public class UploadToDBController {

    @Autowired
    private FilesService filesService;


    @PostMapping("/uploadFile")
    public Files inputFile(@RequestParam("file") MultipartFile file) {

        Files files = new Files();
        if (null != file) {
            String name = file.getOriginalFilename();
            byte[] bytes;
            try {
                bytes = file.getBytes();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            files.setName(name);
            files.setFile(bytes);
            filesService.save(files);
        }
        return files;
    }



}
